include:
  - openstack.queen.keystone.install

nova-control-pkg:
  pkg.installed:
    - names:
      - openstack-nova-api
      - openstack-nova-placement-api
      - openstack-nova-conductor
      - openstack-nova-console 
      - openstack-nova-novncproxy
      - openstack-nova-scheduler
      - httpd
      - mod_wsgi

nova-control-nova-conf:
  file.managed:
    - name: /etc/nova/nova.conf
    - source: salt://openstack/queen/nova-control/files/nova.conf
    - user: root
    - group: nova
    - mode: 640
    - template: jinja
    - defaults:
      MYSQL_IP: {{ pillar['mysql']['MYSQL_IP'] }}
      NOVA_DB_PASS: {{ pillar['nova']['NOVA_DB_PASS'] }}
      NOVA_DB_USER: {{ pillar['nova']['NOVA_DB_USER'] }}
      NOVA_DB_NAME: {{ pillar['nova']['NOVA_DB_NAME'] }}
      NOVA_API_DB_NAME: {{ pillar['nova']['NOVA_API_DB_NAME'] }}
      RABBITMQ_OPENSTACK_USER: {{ pillar['rabbitmq']['RABBITMQ_OPENSTACK_USER'] }}
      RABBITMQ_OPENSTACK_PASS: {{ pillar['rabbitmq']['RABBITMQ_OPENSTACK_PASS'] }}
      RABBITMQ_IP: {{ pillar['rabbitmq']['RABBITMQ_IP'] }}
      MEMCACHED_IP: {{ pillar['memcached']['MEMCACHED_IP'] }}
      NOVA_VNC_IP: {{ pillar['nova']['NOVA_VNC_IP'] }}
      GLANCE_IP: {{ pillar['glance']['GLANCE_IP'] }}
      KEYSTONE_IP: {{ pillar['keystone']['KEYSTONE_IP'] }}
      NOVA_KEYSTONE_USER: {{ pillar['nova']['NOVA_KEYSTONE_USER'] }}
      NOVA_KEYSTONE_PASS: {{ pillar['nova']['NOVA_KEYSTONE_PASS'] }}
      PLACEMENT_KEYSTONE_USER: {{ pillar['placement']['PLACEMENT_KEYSTONE_USER'] }}
      PLACEMENT_KEYSTONE_PASS: {{ pillar['placement']['PLACEMENT_KEYSTONE_PASS'] }}
      NEUTRON_KEYSTONE_USER: {{ pillar['neutron']['NEUTRON_KEYSTONE_USER'] }}
      NEUTRON_KEYSTONE_PASS: {{ pillar['neutron']['NEUTRON_KEYSTONE_PASS'] }}
      NEUTRON_METADATA_SECRET: {{ pillar['neutron']['NEUTRON_METADATA_SECRET'] }}
      NEUTRON_IP: {{ pillar['neutron']['NEUTRON_IP'] }}
    - require:
      - pkg: nova-control-pkg



nova-placement-http-service:
  service.running:
    - name: httpd
    - enable: True
    - reload: True


nova-placement-api-conf:
  file.managed:
    - name: /etc/httpd/conf.d/00-nova-placement-api.conf
    - source: salt://openstack/queen/nova-control/files/00-nova-placement-api.conf
    - user: root
    - group: root
    - mode: 640
    - template: jinja
    - require:
      - pkg: nova-control-pkg
    - watch_in:
      - service: nova-placement-http-service

nova-api-db-sync:
  cmd.run:
    - name: su -s /bin/sh -c "nova-manage api_db sync" nova && touch /etc/lock/nova-api-db-sync.lock
    - require:
      - file: nova-control-nova-conf
    - unless : test -f /etc/lock/nova-api-db-sync.lock

nova-cell0-db-register:
  cmd.run:
    - name: su -s /bin/sh -c "nova-manage cell_v2 map_cell0" nova && touch /etc/lock/nova-cell0-db-register.lock
    - require:
      - cmd: nova-api-db-sync
    - unless : test -f /etc/lock/nova-cell0-db-register.lock

nova-cell1-cell-create:
  cmd.run:
    - name: su -s /bin/sh -c "nova-manage cell_v2 create_cell --name=cell1 --verbose" nova && touch /etc/lock/nova-cell1-cell-create.lock
    - require:
      - cmd: nova-cell0-db-register
    - unless : test -f /etc/lock/nova-cell1-cell-create.lock

nova-db-sync:
  cmd.run:
    - name: su -s /bin/sh -c "nova-manage db sync" nova && touch /etc/lock/nova-db-sync.lock
    - require:
      - cmd: nova-cell1-cell-create
    - unless : test -f /etc/lock/nova-db-sync.lock

nova-api-service:
  service.running:
    - name: openstack-nova-api
    - enable: True
    - require:
      - cmd: nova-db-sync
    - watch:
      - file: nova-control-nova-conf

nova-consoleauth-service:
  service.running:
    - name: openstack-nova-consoleauth
    - enable: True
    - require:
      - cmd: nova-db-sync
    - watch:
      - file: nova-control-nova-conf

nova-scheduler-service:
  service.running:
    - name: openstack-nova-scheduler
    - enable: True
    - require:
      - cmd: nova-db-sync
    - watch:
      - file: nova-control-nova-conf

nova-conductor-service:
  service.running:
    - name: openstack-nova-conductor
    - enable: True
    - require:
      - cmd: nova-db-sync
    - watch:
      - file: nova-control-nova-conf

nova-novncproxy-service:
  service.running:
    - name: openstack-nova-novncproxy
    - enable: True
    - require:
      - cmd: nova-db-sync
    - watch:
      - file: nova-control-nova-conf


nova-service-endpoint:
  file.managed:
    - name: /etc/script/nova-service-endpoint.sh
    - source: salt://openstack/queen/nova-control/files/nova-service-endpoint.sh
    - user: root
    - group: root
    - mode: 700
    - template: jinja
    - defaults:
      NOVA_IP: {{ pillar['nova']['NOVA_IP'] }}
      PLACEMENT_IP: {{ pillar['placement']['PLACEMENT_IP'] }}
  cmd.run:
    - name: bash /etc/script/nova-service-endpoint.sh && touch /etc/lock/nova-service-endpoint.lock
    - require:
      - service: nova-api-service
      - service: nova-scheduler-service
      - service: nova-consoleauth-service
      - service: nova-conductor-service
      - service: nova-novncproxy-service
    - unless : test -f /etc/lock/nova-service-endpoint.lock
